<?php
class post extends spController
{
	function index(){ // 这里是首页
		$this->tpl_title = "文章管理";
		// 按分类浏览文章跳转菜单
		$conditions = "type='category'";
		$cate = spClass("lib_metas");
        $categorys = $cate->findAll($conditions);
		$treeObj = new Tree(array('mid','parent','name'));
		$this->cates = $treeObj->getTree($categorys);
		// 浏览文章		
		$post= spClass("lib_contents");  
		$this->results = $post->spPager($this->spArgs('page', 1), 20)->findAll(array("type" => 'post'),"cid DESC");   
		$this->select = spClass("lib_metas")->findAll();
		$this->pager = $post->spPager()->getPager();
		$this->display("admin/post_index.html");
	}
	
	function write(){ // 撰写新文章
		$this->tpl_title = "新建文章";
		$this->formUrl = spUrl("post", "add");
		$conditions = "type='category'";
		$cate = spClass("lib_metas");
        $categorys = $cate->findAll($conditions);
		$treeObj = new Tree(array('mid','parent','name'));
		$this->results = $treeObj->getTree($categorys);
		$this->display("admin/post_write.html");
	}	

	function edit(){ // 修改
		$this->tpl_title = "编辑文章";
		$this->formUrl = spUrl("post", "update");
		$conditions = "type='category'";
		$cate = spClass("lib_metas");
        $categorys = $cate->findAll($conditions);
		$treeObj = new Tree(array('mid','parent','name'));
		$this->results = $treeObj->getTree($categorys);
		// 这里先判断是否传入了id
		if( $cid = $this->spArgs("cid") ){
			// 查找表，获取信息
			$this->info = spClass("lib_contents")->find(array('cid'=>$cid));
		}else{
			// 无id则直接跳转回首页
			$this->jump(spUrl("post","index"));
		}
		$this->display("admin/post_edit.html");
	}	
	
	function add(){ // 增加
		$post = spClass("lib_contents");
		$newrow = array( // 这里制作新增记录的值
			'mid' => $this->spArgs('mid'), 
			'title' => $this->spArgs('title'),
			'slug' => $this->spArgs('slug'),
			'thumb' => $this->spArgs('thumb'),
			'source' => $this->spArgs('source'),
			'created' => mktime(),
			'keywords' => $this->spArgs('keywords'),
			'text' => $this->spArgs('text'),
			'hits' => $this->spArgs('hits'),
			'url' => $this->spArgs('url'),
			'jump' => $this->spArgs('jump'),
			'status' => $this->spArgs('status'),
			'ping' => $this->spArgs('ping'),
			'comment' => $this->spArgs('comment'),
			'recommend' => $this->spArgs('recommend'),
			'sort' => $this->spArgs('sort'),
			'overview' => $this->spArgs('overview'),
			'uid' => $_SESSION["userinfo"]["uid"],
			'type' => 'post',
		);
		$post->create($newrow); 
		$this->success("文章撰写成功！", spUrl("post", "index"));
	}	

	function update(){ //更新
		$post = spClass("lib_contents");
		// 这里先判断是否传入了id
		if( $cid = $this->spArgs("cid") ){
			// 执行删除
		$conditions = array("cid"=>$cid);
		$newrow = array(
			'mid' => $this->spArgs('mid'), 
			'title' => $this->spArgs('title'),
			'slug' => $this->spArgs('slug'),
			'thumb' => $this->spArgs('thumb'),
			'source' => $this->spArgs('source'),
			'modified' => mktime(),
			'keywords' => $this->spArgs('keywords'),
			'text' => $this->spArgs('text'),
			'hits' => $this->spArgs('hits'),
			'url' => $this->spArgs('url'),
			'jump' => $this->spArgs('jump'),
			'status' => $this->spArgs('status'),
			'ping' => $this->spArgs('ping'),
			'comment' => $this->spArgs('comment'),
			'recommend' => $this->spArgs('recommend'),
			'sort' => $this->spArgs('sort'),
			'overview' => $this->spArgs('overview'),
			'uid' => $_SESSION["userinfo"]["uid"],
			);
		$post->update($conditions, $newrow); // 更新记录
		$this->success("更新文章成功！", spUrl("post", "index"));
		}
	}
	
	function search(){ // 文章搜索
		$this->tpl_title = "文章管理";
		$post= spClass("lib_contents");  
		$keyword = $this->spArgs("keyword");
		$conditions = "type = 'post' AND title like '%".$keyword."%'";
		$this->results = $post->spPager($this->spArgs('page', 1), 10)->findAll($conditions,"cid DESC"); 
		$this->sum = $post->findCount($conditions); // 使用了findCount
		$this->select = spClass("lib_metas")->findAll();
		$this->pager = $post->spPager()->getPager();
		$this->display("admin/post_search.html");
	}
	
	function cate(){ // 浏览分类下文章
		$this->tpl_title = "文章管理";
		// 按分类浏览文章跳转菜单
		$conditions = "type='category'";
		$cate = spClass("lib_metas");
        $categorys = $cate->findAll($conditions);
		$treeObj = new Tree(array('mid','parent','name'));
		$this->cates = $treeObj->getTree($categorys);
		// 浏览分类下文章		
		$post= spClass("lib_contents");  
		$mid = $this->spArgs("mid");
		$this->mid = $this->spArgs("mid");
		$this->results = $post->spPager($this->spArgs('page', 1), 20)->findAll(array("type" => 'post', "mid" =>$mid));   
		$this->select = spClass("lib_metas")->findAll();
		$this->pager = $post->spPager()->getPager();
		$this->display("admin/post_cate.html");
	}	

	function check(){
		// 接收提交的slug值
		$slug = $this->spArgs('slug');
		$results = spClass('lib_contents')->find(array( 'slug' => $slug )); // 查找
		$slugs = $results['slug'];

		if( $slug == $slugs ){
			$result = array(
				'status' => 0, // 失败标志
				'message' => '<span class="input-notification error png_bg">缩略名重复</span>', // 提示信息
			);		
		}else{
			$result = array(
				'status' => 1, // 成功标志
				'message' => '<span class="input-notification success png_bg">缩略名可用</span>', // 提示信息
			);	
		}
		echo json_encode( $result ); // 返回（显示）JSON结果
	}
	
	function del(){// 删除
		// 这里先判断是否传入了id
		if( $cid = $this->spArgs("cid") ){
			// 执行删除
			spClass("lib_contents")->delete(array('cid'=>$cid));
			$this->success("删除成功！", spUrl("post","index"));
		}else{
			// 无id则直接跳转回首页
			$this->jump(spUrl("post","index"));
		}
	}
	
}